<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-goods"></i> 出售管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box" style="margin-bottom:50px">
                <el-col :span="6">
                    <el-button
                        type="danger"
                        icon="el-icon-delete"
                        class="handle-del mr10"
                        @click="delAllSelection"
                    >批量删除</el-button>
                    <el-button
                        type="primary"
                        icon="el-icon-circle-plus-outline"
                        class="handle-del mr10"
                        @click="handleEdit(-1,null)"
                    >添加出售</el-button>
                </el-col>
                <el-col :span="12">
                    <el-select v-model="query.sort" placeholder="排序" class="handle-select mr10">
                        <el-option key="1" label="正序" value="1"></el-option>
                        <el-option key="2" label="倒序" value="2"></el-option>
                    </el-select>
                    <el-select v-model="query.status" placeholder="状态" class="handle-select mr10">
                        <el-option label="全部" value></el-option>
                        <el-option key="1" label="正常" value="1"></el-option>
                        <el-option key="2" label="禁用" value="2"></el-option>
                    </el-select>
                    <el-input v-model="query.name" placeholder="商品标题" class="handle-input mr10"></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                </el-col>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                v-loading="loading"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="70" sortable align="center"></el-table-column>
                <!--el-table-column label="类型">
                    <template
                        slot-scope="scope"
                    >{{scope.row.type==1?'正常显示':(scope.row.type==2?'跳转网址':'打开出售')}}</template>
                </el-table-column-->
                <el-table-column prop="title" label="商品标题"></el-table-column>
                <el-table-column prop="qu_name" label="区服"></el-table-column>
                <el-table-column label="排序" prop="sort" sortable="custom"></el-table-column>
                <el-table-column label="图片(查看大图)" align="center">
                    <template slot-scope="scope">
                        <el-image
                            class="table-td-thumb"
                            :src="scope.row.images[0]"
                            :preview-src-list="[scope.row.images]"
                        ></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="状态" sortable align="center">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.status==1" type="success">正常</el-tag>
                        <el-tag v-if="scope.row.status==2" type="danger">禁用</el-tag>
                        <el-tag v-if="scope.row.status==3" type="danger">已售罄</el-tag>
                    </template>
                </el-table-column>

                <el-table-column prop="updated_at" sortable label="修改时间"></el-table-column>
                <el-table-column prop="created_at" sortable label="添加时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.page"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 添加弹出框 -->
        <el-dialog :title="formName" v-dialogDrag :visible.sync="editVisible" width="70%">
            <el-form ref="edit" :model="edit" label-width="90px">
                <el-form-item label="商品标题" class="form-item">
                    <el-input v-model="edit.title"   placeholder="请输入商品标题"></el-input>
                </el-form-item>
                <el-form-item label="商品描述" label-width="80px">
                    <editor-bar v-model="edit.describe" :isClear="true" @change="contentChange2"></editor-bar>
                </el-form-item>
                <el-form-item label="选择游戏" class="form-item">
                    <el-select
                            v-model="edit.game_id"
                            placeholder="选择游戏"
                            style="width:120px;margin-right:10px"
                            @change="showGame"

                    >
                        <template v-for="(item) in game_list">
                            <el-option
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id"
                            ></el-option>
                        </template>
                    </el-select>
                    <el-button @click="edit.game_id =''">重置</el-button>

                </el-form-item>
                <el-form-item label="选择大区" class="form-item" v-if="edit.game_id == 3">
                    <el-select
                            v-model="edit.qu_name"
                            placeholder="选择大区"
                            style="width:120px;margin-right:10px"
                            @change="showQu"

                    >
                        <template v-for="(item) in qu_list1">
                            <el-option
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id"
                            ></el-option>
                        </template>
                    </el-select>
                </el-form-item>
                <el-form-item label="选择大区" class="form-item" v-else>
                    <el-select
                            v-model="edit.qu_name"
                            placeholder="选择大区"
                            style="width:120px;margin-right:10px"
                            @change="showQu"

                    >
                        <template v-for="(item) in qu_list">
                            <el-option
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id"
                            ></el-option>
                        </template>
                    </el-select>
                </el-form-item>
                <el-form-item label="游戏账号" class="form-item">
                <el-input v-model="edit.game_zhanghao" maxlength="100" minlength="1" placeholder="请输入游戏账号"></el-input>
                </el-form-item>
                <el-form-item label="账号属性" class="form-item" v-if="edit.game_id == 1">
                    <el-row>
                        <el-col>
                            <span>贵族</span>
                            <el-select
                                    v-model="edit.game_attr.vip"
                                    placeholder="选择贵族"
                                    style="width:120px;margin-left:10px"
                                    @change="showVip"

                            >
                                <template v-for="(item) in vip_list">
                                    <el-option
                                            :key="item.id"
                                            :label="item.name"
                                            :value="item.id"
                                    ></el-option>
                                </template>
                            </el-select>
                        </el-col>
                        <el-col>
                            <span>星元皮肤数</span>
                            <el-input
                                    type="text"
                                    v-model="edit.game_attr.xy_pifu"
                                    maxlength="60"
                                    minlength="1"
                                    placeholder="星元皮肤数"
                            ></el-input>
                        </el-col>
                        <el-col>
                            <span>皮肤数量</span>
                            <el-input
                                    type="text"
                                    v-model="edit.game_attr.pifu_num"
                                    maxlength="60"
                                    minlength="1"
                                    placeholder="皮肤数量"
                            ></el-input>
                        </el-col>
                        <el-col>
                            <span>英雄数量</span>
                            <el-input
                                    type="text"
                                    v-model="edit.game_attr.hero_num"
                                    maxlength="60"
                                    minlength="1"
                                    placeholder="英雄数量"
                            ></el-input>
                        </el-col>
                        <el-col>
                            <span>荣耀典藏</span>
                            <el-input
                                    type="text"
                                    v-model="edit.game_attr.rydchang_num"
                                    maxlength="60"
                                    minlength="1"
                                    placeholder="荣耀典藏数量"
                            ></el-input>
                        </el-col>
                        <el-col>
                            <span>实名信息</span>
                            <el-select
                                    v-model="edit.game_attr.is_auth"
                                    placeholder="实名信息"
                                    style="width:120px;margin-right:10px"
                                    @change="showAuth"

                            >
                                <template v-for="(item) in auth_list">
                                    <el-option
                                            :key="item.id"
                                            :label="item.name"
                                            :value="item.id"
                                    ></el-option>
                                </template>
                            </el-select>
                        </el-col>
                        <el-col>
                            <span>防沉迷</span>
                            <el-select
                                    v-model="edit.game_attr.is_fcm"
                                    placeholder="防沉迷"
                                    style="width:120px;margin-right:10px"
                                    @change="showFcm"

                            >
                                <template v-for="(item) in fcm_list">
                                    <el-option
                                            :key="item.id"
                                            :label="item.name"
                                            :value="item.id"
                                    ></el-option>
                                </template>
                            </el-select>
                        </el-col>
                    </el-row>
                </el-form-item>
                <el-form-item label="账号属性" class="form-item" v-if="edit.game_id == 2">
                    <el-row>
                        <el-col>
                            <span>载具数量</span>
                            <el-input
                                    type="text"
                                    v-model="edit.game_attr.zaiju_num"
                                    maxlength="60"
                                    minlength="1"
                                    placeholder="载具数量"
                            ></el-input>
                        </el-col>
                        <el-col>
                            <span>特效数量</span>
                            <el-input
                                    type="text"
                                    v-model="edit.game_attr.tenxiao_num"
                                    maxlength="60"
                                    minlength="1"
                                    placeholder="特效数量"
                            ></el-input>
                        </el-col>
                        <el-col>
                            <span>粉装数量</span>
                            <el-input
                                    type="text"
                                    v-model="edit.game_attr.fengzhuan_num"
                                    maxlength="60"
                                    minlength="1"
                                    placeholder="粉装数量"
                            ></el-input>
                        </el-col>
                        <el-col>
                            <span>王牌印记</span>
                            <el-input
                                    type="text"
                                    v-model="edit.game_attr.wangpaiyj_num"
                                    maxlength="60"
                                    minlength="1"
                                    placeholder="王牌印记数量"
                            ></el-input>
                        </el-col>
                        <el-col>
                            <span>实名信息</span>
                            <el-select
                                    v-model="edit.game_attr.is_auth"
                                    placeholder="实名信息"
                                    style="width:120px;margin-right:10px"
                                    @change="showAuth"

                            >
                                <template v-for="(item) in auth_list">
                                    <el-option
                                            :key="item.id"
                                            :label="item.name"
                                            :value="item.id"
                                    ></el-option>
                                </template>
                            </el-select>
                        </el-col>
                        <el-col>
                            <span>防沉迷</span>
                            <el-select
                                    v-model="edit.game_attr.is_fcm"
                                    placeholder="防沉迷"
                                    style="width:120px;margin-right:10px"
                                    @change="showFcm"

                            >
                                <template v-for="(item) in fcm_list">
                                    <el-option
                                            :key="item.id"
                                            :label="item.name"
                                            :value="item.id"
                                    ></el-option>
                                </template>
                            </el-select>
                        </el-col>
                    </el-row>
                </el-form-item>
                <el-form-item label="出售价格" class="form-item">
                    <el-input v-model="edit.price"   placeholder="请输入出售价格"></el-input>
                </el-form-item>
                    <el-form-item label="状态">
                    <el-select v-model="edit.status" placeholder="请选择状态">
                        <el-option label="正常" value="1"></el-option>
                        <el-option label="禁用" value="2"></el-option>
                        <el-option label="已售罄" value="3"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="排序" class="form-item">
                    <el-input
                        v-model="edit.sort"
                        maxlength="30"
                        type="number"
                        minlength="2"
                        placeholder="请输入排序"
                    ></el-input>
                    <p style="color: red;font-size: 13px">注：越小越靠前</p>

                </el-form-item>
                <el-form-item label="上传游戏内截图">
                    <el-upload
                        class="upload-demo"
                        ref="addUploads"
                        drag
                        :limit="10"
                        :auto-upload="true"
                        show-file-list
                        accept="image/*"
                        :on-success="uploadSuccess"
                        :on-error="uploadError"
                        :on-preview="uploadPreview"
                        :before-upload="beforeAvatarUpload"
                        :action="api.upload_img_url"
                        list-type="picture"
                        :on-exceed="uploadExceed"
                        :file-list="edit.img_url"
                        multiple
                    >
                        <i class="el-icon-upload"></i>
                        <div class="el-upload__text">
                            将文件拖到此处，或
                            <em>点击上传</em>
                        </div>
                        <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过5M</div>
                    </el-upload>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleSave">确 定</el-button>
            </span>
        </el-dialog>
        <!--出售属性-->
        <!-- 预览图片 -->
        <div class="previews" @click="closePreviews" v-if="windowsPopVisible">
            <img :src="windowsPopUrl" title="点击即可关闭预览" />
        </div>
    </div>
</template>

<script>
    import EditorBar from '../../common/wangEnduit';

    export default {
        components: { EditorBar },
        name: 'sell',
  data () {
      const item = {
          date: '',
          name: '',
          address: ''
      };
    return {
      query: {
        status: '',
        sort: '',
        name: '',
        page: 1,
        pageSize: 10
      },
      loading: false,
      cate_loading: false,
      attr_loading: false,
      tableData: [],
      windowsPopVisible: false,
      windowsPopUrl: '',
      multipleSelection: [],
      delList: [],
      editVisible: false,
      addVisible: false,

      sellVisible: false,
      vip_list:[
            {'id':"1","name":"V2"},
            {"id":"2","name":"V3"},
            {"id":"3","name":"V4"},
          {"id":"4","name":"V5"},
          {"id":"5","name":"V6"},
          {"id":"6","name":"V7"},
          {"id":"7","name":"V8"},
          {"id":"8","name":"V9"},
          {"id":"9","name":"V10"},

      ],
      auth_list:[
          {'id':"1","name":"可修改实名"},
          {'id':"2","name":"不可修改实名"},
      ],
      fcm_list:[
            {'id':"1","name":"有防沉迷"},
            {'id':"2","name":"无防沉迷"},
      ],
      qu_list:[
            {'id':"1","name":"安卓QQ"},
            {'id':"2","name":"安卓微信"},
            {'id':"3","name":"苹果微信"},
            {'id':"4","name":"苹果QQ"},
      ],
      qu_list1:[
            {'id':"1","name":"安卓"},
            {'id':"2","name":"IOS"},
        ],


        pageTotal: 0, catepageTotal: 0,AttrpageTotal: 0,
        edit: {
          game_attr: {
              xy_pifu: '',
              is_auth: '',
              is_fcm: '',
              vip: '',
              pifu_num: '',
              hero_num: '',
              rydchang_num: '',
              zaiju_num: '',
              tenxiao_num: '',
              fengzhuan_num: '',
              wangpaiyj_num: '',
          },
            status: '',
            title: '',
            describe:'',
            game_id:'请选择',
            game_zhanghao:'',
            sort: 10,
            images: [],
            qu_name:'',
        },
      idx: -1,
      id: -1,

        game_list:[],
        formName: '添加出售',
        getList: function () {
        let that = this;
        this.api.get('getSell', this.query, res => {
          for (let i in res.data.list) {
            res.data.list[i].status = res.data.list[i].status + '';
          }
          that.tableData = res.data.list;
          that.pageTotal = res.data.pageTotal;
        }, undefined, undefined, this);

      },
      delBanner: function (ids, msg) {
        let that = this;
        if (!ids || ids.length < 1) {
          this.$message.error('请选择删除的数据');
          return false;
        }
        // 二次确认删除
        this.$confirm('确定要删除吗？', '提示', {
          type: 'warning'
        }).then(() => {
          that.api.delete('delSell', { ids: ids }, res => {
            that.multipleSelection = [];
            that.query.page = 1;
            that.getList();
            that.$message.success(msg);
          });
        }).catch(() => { });
      },
        delCate: function (ids, msg) {
            let that = this;
            if (!ids || ids.length < 1) {
                this.$message.error('请选择删除的数据');
                return false;
            }
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                that.api.delete('delGameCate', { ids: ids }, res => {
                    that.catemultipleSelection = [];
                    that.cate_query.page = 1;
                    that.gameCateAllList();
                    that.$message.success(msg);
                });
            }).catch(() => { });
        },
        delCateAttr: function (ids, msg) {
            let that = this;
            if (!ids || ids.length < 1) {
                this.$message.error('请选择删除的数据');
                return false;
            }
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                that.api.delete('delGameCateAttr', { ids: ids }, res => {
                    that.cateattrmultipleSelection = [];
                    that.attr_query.page = 1;
                    that.gameCateAttrList()
                    that.$message.success(msg);
                });
            }).catch(() => { });
        },
    };
  },

  created () {
  this.api.get('getGoodsAll', {}, res => {
      this.game_list = res.data.list;
  });
    this.getList();
  },
  methods: {
    uploadExceed () {
      this.$message.error('只能上传一张图片');
    },
      contentChange2 (val) {
          this.edit.describe = val;
      },
    /***
     * 添加banner预览
     */
    uploadPreview (file) {
      this.windowsPopVisible = true;
      this.windowsPopUrl = file.response.data.url;
    },

    /***
     * 关闭预览
     */
    closePreviews () {
      this.windowsPopVisible = false;
    },

    /***
     * 添加banner上传成功事件
     */
    uploadSuccess (response, file, fileList) {
        if (response.code != 1000) {
            this.$message.error(response.msg);
            return false;
        }
        let images = [];
        for (let i in fileList) {
            images[images.length] = fileList[i].response.data.url;
        }
        this.edit.images = images;
    },
    /***
     * 添加banner上传失败事件
     */
    uploadError (response, file, fileList) {
      this.$message.error('上传失败');
    },

    /****
     * 编辑banner预览
     */
    editUploadPreview (info) {
      this.windowsPopUrl = info.url;
      this.windowsPopVisible = true;
    },

    /***
     * 编辑上传成功事件
     */
    editUploadSuccess (response) {
      if (response.code != 1000) {
        this.$message.error(response.msg);
        return false;
      }
      this.edit.img = response.data.path;
      this.edit.img_url = response.data.url;
    },
      beforeAvatarUpload(file) {
          const isLt2M = file.size / 1024 / 1024 < 5;
          if (!isLt2M) {
              this.$message.error('上传大小不能超过5MB');
          }
          return isLt2M;
      },
    /***
     * 编辑上传失败事件
     */
    editUploadError () {
      this.$message.error('上传失败');
    },

    // 触发搜索按钮
    handleSearch () {
      this.$set(this.query, 'page', 1);
      this.getList();
    },
    // 删除操作
    handleDelete (index, row) {
      this.delBanner([row.id], '删除成功');
    },
      cateChildClick (id=0) {
        if (id>0){
            this.attr_query.left_two_id = id
        }
          this.gameCateAttrList();
      },
    // 多选操作
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },
    delAllSelection () {
      const length = this.multipleSelection.length;
      let str = '';
      this.delList = this.delList.concat(this.multipleSelection);
      let ids = [];
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + ' ';
        ids.push(this.multipleSelection[i].id);
      }
      this.delBanner(ids, `删除了${str}`);
    },

      CatedelSelection () {
      const length = this.catemultipleSelection.length;
      let str = '';
      this.catedelList = this.catedelList.concat(this.catemultipleSelection);
      let ids = [];
      for (let i = 0; i < length; i++) {
        str += this.catemultipleSelection[i].name + ' ';
        ids.push(this.catemultipleSelection[i].id);
      }
      this.delCate(ids, `删除了${str}`);
    },
      CatedelattrSelection () {
          const length = this.cateattrmultipleSelection.length;
          let str = '';
          this.cateattrdelList = this.cateattrdelList.concat(this.cateattrmultipleSelection);
          let ids = [];
          for (let i = 0; i < length; i++) {
              str += this.cateattrmultipleSelection[i].name + ' ';
              ids.push(this.cateattrmultipleSelection[i].id);
          }
          this.delCate(ids, `删除了${str}`);
      },
    // 编辑操作
    handleEdit (index, row) {

      // this.idx = index;
      // this.edit = row;
      // this.editVisible = true;
        if (index < 0) {
            this.formName = '添加出售';
            this.edit = {
                game_attr:{
                    xy_pifu:'',
                    is_auth:'',
                    is_fcm:'',
                    vip:'',
                    pifu_num:'',
                    hero_num:'',
                    rydchang_num:'',
                    zaiju_num:'',
                    tenxiao_num:'',
                    fengzhuan_num:'',
                    wangpaiyj_num:'',
                },
                status: '',
                title: '',
                describe:'',
                game_id:'请选择',
                game_zhanghao:'',
                sort: 10,
                images: [],
                qu_name:'',
            }
        } else {
            this.formName = '编辑出售';
            console.log(row)
            this.edit = row;
            let images = [];
            for (let i in row.images) {
                images[images.length] = {
                    name: '预览',
                    url: row.images[i]
                };
            }
            row.game_id = row.game_id.toString();
            this.edit = row;
            this.edit.img_url = images;

            if (this.edit.info){
                this.edit.info
            }
        }
        this.editVisible = true;
    },

    // 保存编辑
    saveEdit () {
      this.api.put('editGame', this.edit, res => {
        this.$message.success('保存成功');
        this.editVisible = false;
        this.getList();
      });

    },
      // 编辑操作
    handleGame (index, row) {
        // this.idx = index;
        // this.editCate = row;
        this.editCate.game_id = row.id;
        console.log(this.editCate.game_id)
        this.editVisible = true;
        this.getCate();
        this.windowsPopUrl = '';
    },
      // 保存编辑
      saveGame () {
          this.api.put('editGame', this.edit, res => {
              this.$message.success('保存成功');
              this.editVisible = false;
              this.getList();
          });

      },
    /***
     * 添加操作
     */
    handleAdd () {
      this.add = {
        num: '',
        img: '',
        name: '',
        status: '',
        sort: 10,
        ticket: '',
        img_url: []
      };
      this.windowsPopUrl = '';
      this.addVisible = true;
    },
    /***
     * 保存添加
     */
    saveAdd () {
      this.api.post('addGame', this.add, res => {
        this.$message.success('添加成功');
        this.addVisible = false;
        this.getList();
      });

    },
    handleSave () {
        this.api.post('editSell', this.edit, res => {
            this.$message.success(res.msg);
            this.editVisible = false;
            this.getList();
        });
      },
    // 分页导航
    handlePageChange (val) {
      this.$set(this.query, 'page', val);
      this.getList();
    },
      // 分页导航
      catehandlePageChange (val) {
          this.$set(this.cate_query, 'page', val);
          this.gameCateAllList();
      },
      // 分页导航
      cateattrhandlePageChange (val) {
          this.$set(this.attr_query, 'page', val);
          this.gameCateAttrList();
      },
      showGame(val){
          this.edit.game_id = val||0
      },
      showQu(val){
          this.edit.qu_name = val||0
      },
      showVip(val){
          this.edit.game_attr.vip = val||0
      },
      showAuth(val){
          this.edit.game_attr.is_auth = val||0
      },
      showFcm(val){
          this.edit.game_attr.is_fcm = val||0
      },

  }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}
.form-item {
    width: 60%;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
<style>
    .el-header {
        background-color: #B3C0D1;
        color: #333;
        line-height: 60px;
    }

    .el-aside {
        color: #333;
    }
</style>
